﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Common.Logging;
using Common.Logging.Configuration;
using Common.Logging.Simple;

namespace Quartz.WebManager.SimpleLog
{
    public class FileLoggerFactoryAdapter : AbstractSimpleLoggerFactoryAdapter
    {
        private readonly string fileName;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLoggerFactoryAdapter"/> class using default 
        /// settings.
        /// </summary>
        public FileLoggerFactoryAdapter()
            : base(null)
        {
            this.fileName = "output.log";
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLoggerFactoryAdapter"/> class.
        /// </summary>
        /// <param name="properties">The name value collection, typically specified by the user in 
        /// a configuration section named common/logging.</param>
        public FileLoggerFactoryAdapter(NameValueCollection properties)
            : base(properties)
        {
            this.fileName = ArgUtils.GetValue(properties, "fileName", "output.log");
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLoggerFactoryAdapter"/> class with 
        /// default settings for the loggers created by this factory.
        /// </summary>
        public FileLoggerFactoryAdapter(LogLevel level, bool showDateTime, bool showLogName, bool showLevel, string dateTimeFormat, string fileName)
            : base(level, showDateTime, showLogName, showLevel, dateTimeFormat)
        {
            this.fileName = fileName ?? "output.log";
        }

        public string FileName
        {
            get { return this.fileName; }
        }

        /// <summary>
        /// Creates a new <see cref="ConsoleOutLogger"/> instance.
        /// </summary>
        protected override ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat)
        {
            ILog log = new FileLogger(name, level, showLevel, showDateTime, showLogName, dateTimeFormat, this.fileName);
            return log;
        }
    }
}
